﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI.WebControls;
using System.Data;

/// <summary>
///ConfigMenus 的摘要说明
/// </summary>
public class ConfigMenus
{
	public ConfigMenus()
	{
        string path = HttpContext.Current.Server.MapPath("~/configs/menu.xml");
        if (!File.Exists(path))
        {
            this.ObjToXml(path);
        }
        this.ID = "0";
        this.PID = "0";
        this.IDinChild = 1;
        this.FromParent = true;
	}

    public string ID { get; set; }
    public string PID { get; set; }
    public int IDinChild { get; set; }
    public string ShowText { get; set; }
    public string TableName { get; set; }
    public string TablePK { get; set; }
    public string TableShowColumnName { get; set; }
    public string TableParentColumnName { get; set; }
    public string TableParentBeginValue { get; set; }
    public string ForgeignTableName { get; set; }
    public string ForgeignTableFKName { get; set; }
    public string XmlName { get; set; }
    public string Url { get; set; }
    public bool AutoChild { get; set; }
    public bool IsSplit { get; set; }
    public bool FromParent { get; set; }
    public string[] AllowRoles { get; set; }
    public string[] AllowAdmins { get; set; }
    public bool UseClass { get; set; }
    public string MenuClassName { get; set; }

    public List<ConfigMenus> ChildMenus { get; set; }

    public void Save()
    {
        string path = HttpContext.Current.Server.MapPath("~/configs/menu.xml");
        this.ObjToXml(path);
    }

    public static ConfigMenus Load()
    {
        string path = HttpContext.Current.Server.MapPath("~/configs/menu.xml");
        if (!File.Exists(path))
        {
            new ConfigMenus().ObjToXml(path);
        }
        return path.XmlToObj<ConfigMenus>();
    }

    public static List<ConfigMenus> AllMenu = new List<ConfigMenus>();
    public ConfigMenus GetMenu(string id)
    {
        ConfigMenus.AllMenu.Clear();
        ConfigMenus.AllMenu.Add(this);
        InputChildMenu(this);

        //throw new Exception(AllMenu.Count.ToString());

        return ConfigMenus.AllMenu.Single(x => x.ID == id);
    }

    void InputChildMenu(ConfigMenus m)
    {
        ConfigMenus.AllMenu.AddRange(m.ChildMenus);
        foreach (ConfigMenus mm in m.ChildMenus)
        {
            InputChildMenu(mm);
        }
    }

    public void SetupTree(TreeView tree)
    {
        if (this.AutoChild)
        {
            SetupAutoChild(tree);
        }
        else if (this.UseClass && !string.IsNullOrEmpty(this.MenuClassName))
        {
            SetupClassMenu(tree);
        }

        SetupAbsChild(tree);
    }

    public void SetupClassMenu(TreeView tree)
    {
        ILeftTreeMenu classmenu = LeftTreeMenuFactory.GetLeftTreeMenuClass(this.MenuClassName);
        List<LeftTreeMenu> list=new List<LeftTreeMenu>();
        if (classmenu != null)
        {
            list = classmenu.GetMenus();
        }
        foreach (LeftTreeMenu m in list)
        {
            TreeNode n = new TreeNode();
            n.Text = m.Name;
            n.NavigateUrl = m.Url;
            n.Target = "main";
            n.SelectAction = TreeNodeSelectAction.SelectExpand;
            tree.Nodes.Add(n);
            SetupClassChild(n, m);
        }
    }

    public void SetupClassChild(TreeNode node, LeftTreeMenu menu)
    {
        if (menu.Childrens.Count > 0)
        {
            node.SelectAction = TreeNodeSelectAction.Expand;
            node.NavigateUrl = string.Empty;
        }
        foreach (LeftTreeMenu m in menu.Childrens)
        {
            TreeNode n = new TreeNode();
            n.Text = m.Name;
            n.Target = "main";
            n.NavigateUrl = m.Url;
            node.ChildNodes.Add(n);
            SetupClassChild(n, m);
        }
    }

    private void SetupAutoChild(TreeView tree)
    {        
        string sql = "select * from " + this.TableName;
        if (!string.IsNullOrEmpty(this.TableParentColumnName))
        {
            sql += " where " + this.TableParentColumnName + "=" + this.TableParentBeginValue;
        }        
        DataTable tab = DBFactory.GetConn().exeTable(sql);
        foreach (DataRow row in tab.Rows)
        {
            TreeNode node = new TreeNode();
            node.Text = row[this.TableShowColumnName].ToString();
            node.Target = "main";
            //node.NavigateUrl = "list.aspx?tab=" + this.ForgeignTableName + "&xml=" + this.XmlName+"&lt="+this.ForgeignTableFKName+"&lv="+row[this.TablePK].ToString();
            ConfigTable cTab = ConfigTable.GetConfigTable(this.ForgeignTableName, this.XmlName);
            string param=(cTab.ListUrl.IndexOf("?") > -1) ? "&" : "?";
            //string typeTab = string.Empty;
            //if (!string.IsNullOrEmpty(this.TableName))
            //{
            //    typeTab = "&typeTab=" + this.TableName;
            //}
            node.NavigateUrl = cTab.ListUrl + param + "lt=" + this.ForgeignTableFKName + "&lv=" + row[this.TablePK].ToString();

            tree.Nodes.Add(node);

            if (!string.IsNullOrEmpty(this.TableParentColumnName))
            {                
                SetupAutoChild(node, row[this.TablePK].ToString());
            }
        }
    }

    private void SetupAutoChild(TreeNode n)
    {
        if (!this.FromParent && ConfigGlobal.GetGlobal().AllowRole)
        {
            string uid = "0";
            string roleid = "0";
            if (HttpContext.Current.Request.Cookies["admin_uid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_uid"].Value))
            {
                uid = HttpContext.Current.Request.Cookies["admin_uid"].Value;
            }
            if (HttpContext.Current.Request.Cookies["admin_roleid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_roleid"].Value))
            {
                roleid = HttpContext.Current.Request.Cookies["admin_roleid"].Value;
            }
            if (!this.AllowRoles.Contains(roleid) && !this.AllowAdmins.Contains(uid))
            {
                return;
            }
        }
        string sql = "select * from " + this.TableName;
        if (!string.IsNullOrEmpty(this.TableParentColumnName))
        {
            sql += " where " + this.TableParentColumnName + "=" + this.TableParentBeginValue;
        }
        DataTable tab = DBFactory.GetConn().exeTable(sql);
        if (tab.Rows.Count > 0)
        {
            n.SelectAction = TreeNodeSelectAction.Expand;
            n.NavigateUrl = string.Empty;
        }
        foreach (DataRow row in tab.Rows)
        {
            TreeNode node = new TreeNode();
            node.Text = row[this.TableShowColumnName].ToString();
            node.Target = "main";
            node.NavigateUrl = "list.aspx?tab=" + this.ForgeignTableName + "&xml=" + this.XmlName + "&lt=" + this.ForgeignTableFKName + "&lv=" + row[this.TablePK].ToString();

            n.ChildNodes.Add(node);

            SetupAutoChild(node, row[this.TablePK].ToString());
        }
    }

    private void SetupAutoChild(TreeNode n, string beginValue)
    {
        if (!this.FromParent)
        {
            if (ConfigGlobal.GetGlobal().AllowRole)
            {
                string uid = "0";
                string roleid = "0";
                if (HttpContext.Current.Request.Cookies["admin_uid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_uid"].Value))
                {
                    uid = HttpContext.Current.Request.Cookies["admin_uid"].Value;
                }
                if (HttpContext.Current.Request.Cookies["admin_roleid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_roleid"].Value))
                {
                    roleid = HttpContext.Current.Request.Cookies["admin_roleid"].Value;
                }
                if (!this.AllowRoles.Contains(roleid) && !this.AllowAdmins.Contains(uid))
                {
                    return;
                }
            }
        }
        string sql = "select * from " + this.TableName;
        if (!string.IsNullOrEmpty(this.TableParentColumnName))
        {
            sql += " where " + this.TableParentColumnName + "=" + beginValue;
        }
        DataTable tab = DBFactory.GetConn().exeTable(sql);
        if (tab.Rows.Count > 0)
        {
            n.SelectAction = TreeNodeSelectAction.Expand;
            n.NavigateUrl = string.Empty;
        }
        foreach (DataRow row in tab.Rows)
        {
            TreeNode node = new TreeNode();
            node.Text = row[this.TableShowColumnName].ToString();
            node.Target = "main";
            //node.NavigateUrl = "list.aspx?tab=" + this.ForgeignTableName + "&xml=" + this.XmlName + "&lt=" + this.ForgeignTableFKName + "&lv=" + row[this.TablePK].ToString();
            ConfigTable cTab = ConfigTable.GetConfigTable(this.ForgeignTableName, this.XmlName);
            string param = (cTab.ListUrl.IndexOf("?") > -1) ? "&" : "?";
            //string typeTab = string.Empty;
            //if (!string.IsNullOrEmpty(this.TableName))
            //{
            //    typeTab = "&typeTab=" + this.TableName;
            //}
            node.NavigateUrl = cTab.ListUrl + param + "lt=" + this.ForgeignTableFKName + "&lv=" + row[this.TablePK].ToString();
            n.ChildNodes.Add(node);

            SetupAutoChild(node, row[this.TablePK].ToString());
        }
    }
    private void SetupAbsChild(TreeView tree)
    {        
        foreach (ConfigMenus m in this.ChildMenus)
        {
            //有不完善的地方
            if (ConfigGlobal.GetGlobal().AllowRole)
            {
                if (!m.FromParent)
                {
                    string uid = "0";
                    string roleid = "0";
                    if (HttpContext.Current.Request.Cookies["admin_uid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_uid"].Value))
                    {
                        uid = HttpContext.Current.Request.Cookies["admin_uid"].Value;
                    }
                    if (HttpContext.Current.Request.Cookies["admin_roleid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_roleid"].Value))
                    {
                        roleid = HttpContext.Current.Request.Cookies["admin_roleid"].Value;
                    }
                    if (!m.AllowRoles.Contains(roleid) && !m.AllowAdmins.Contains(uid))
                    {
                        continue;
                    }
                }
            }

            TreeNode node = new TreeNode();
            node.Text = m.ShowText;
            if (!m.AutoChild)
            {
                node.NavigateUrl = m.Url;
                node.Target = "main";
            }
            tree.Nodes.Add(node);
            if (m.AutoChild)
            {
                m.SetupAutoChild(node);
            }
            else if (this.UseClass && !string.IsNullOrEmpty(this.MenuClassName))
            {
                ILeftTreeMenu classmenu = LeftTreeMenuFactory.GetLeftTreeMenuClass(this.MenuClassName);
                List<LeftTreeMenu> list = new List<LeftTreeMenu>();
                if (classmenu != null)
                {
                    list = classmenu.GetMenus();
                }
                LeftTreeMenu temp = new LeftTreeMenu();
                temp.Childrens = list;
                SetupClassChild(node, temp);
            }
            if (m.ChildMenus.Count > 0)
            {
                m.SetupAbsChild(node);
            }            
        }
    }

    private void SetupAbsChild(TreeNode n)
    {
        n.SelectAction = TreeNodeSelectAction.Expand;
        n.NavigateUrl = string.Empty;
        foreach (ConfigMenus m in this.ChildMenus)
        {
            if (!m.FromParent)
            {                
                string uid = "0";
                string roleid = "0";
                if (HttpContext.Current.Request.Cookies["admin_uid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_uid"].Value))
                {
                    uid = HttpContext.Current.Request.Cookies["admin_uid"].Value;
                }
                if (HttpContext.Current.Request.Cookies["admin_roleid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_roleid"].Value))
                {
                    roleid = HttpContext.Current.Request.Cookies["admin_roleid"].Value;
                }
                if (!m.AllowRoles.Contains(roleid) && !m.AllowAdmins.Contains(uid))
                {
                    continue;
                }
            }

            TreeNode node = new TreeNode();
            node.Text = m.ShowText;
            if (!m.AutoChild)
            {
                node.NavigateUrl = m.Url;
                node.Target = "main";
            }
            n.ChildNodes.Add(node);
            if (m.AutoChild)
            {
                m.SetupAutoChild(node);
            }
            if (m.ChildMenus.Count > 0)
            {
                m.SetupAbsChild(node);
            }   
        }
    }
}
